package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//解决跨域问题
@CrossOrigin
@RestController
@RequestMapping(value = "/company")
public class  CompanyController {
    @Autowired
    private CompanyService companyService;
    //CRUD和查询全部的企业
    //1.保存
    @RequestMapping(value = "",method = RequestMethod.POST)
    public  Result save(@RequestBody Company company){
        companyService.add(company);
        return  new Result(ResultCode.SUCCESS);
    }
    //2.更新
    /*
    * 1.方法
    * 2.请求参数
    * 3.响应
    *
    * */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result  update(@PathVariable(value = "id") String id,@RequestBody Company company){
        //业务 操作
        company.setId(id);
        companyService.update(company);
        return  new Result(ResultCode.SUCCESS);
    }
    //根据id删除
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id){
        companyService.deleteById(id);
        return  new Result(ResultCode.SUCCESS);
    }

    //根据id查询企业
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public  Result  findById(@PathVariable(value = "id") String id){
        Company company = companyService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(company);
        return result;
    }

    //查询全部企业列表
    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result  findAll(){
        List<Company> list = companyService.findAll();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return result;
    }
}
